package com.zycx.common.utils;

import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Description:  对象转换工具
 * Created by zycx on 2018-12-05 22:07
 * Created with IntelliJ IDEA.
 */
public class ConversionUtils  extends LinkedHashMap<String,Object> {

    private static Mapper MAPPER = DozerBeanMapperBuilder.buildDefault();

    /**
     *  map 转 Object
     * @param source 传入对象
     * @param destinationClass  目标对象
     * @param <T> T
     * @return Object
     */
    public static <T> T mapToObject(Object source, Class<T> destinationClass) {
        if (source == null) {
            return null;
        }
        return MAPPER.map(source, destinationClass);
    }

    public static void map(Object source, Object destination) {
        MAPPER.map(source, destination);
    }

    /***
     *
     * @param sourceList 集合
     * @param destinationClass liste
     * @param <T>
     * @return
     */
    public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass) {
        List<T> destinationList = new ArrayList<>();
        for (Object sourceObject : sourceList) {
            destinationList.add(MAPPER.map(sourceObject, destinationClass));
        }
        return destinationList;
    }


}
